/*
 * EncounterWatcher.java
 *
 * Created on August 25, 2007, 5:32 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.lct.eq2.watchers;

import com.lct.eq2.data.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Kris
 */
public class EncounterWatcher extends Thread implements Runnable {
    private boolean encounterActive = false;
    private boolean running = false;
    private boolean forced = false;
    private static Encounter parentEncounter;
    private static DataHandler parentHandler;
    private int waitTime = 0;
    private int count = 0;
    
    /** Creates a new instance of EncounterWatcher */
    public EncounterWatcher(DataHandler parentHandler, Encounter parentEncounter, int waitTime) {
        this.running = true;
        this.encounterActive = true;
        this.waitTime = waitTime;
        this.parentEncounter = parentEncounter;
        this.parentHandler = parentHandler;
    }
    
    public void refreshEncounter() {
        this.running = true;
        this.count = 0;
        this.encounterActive = true;
    }
    
    public boolean getEncounterActive() {
        return this.encounterActive;
    }
    
    public void setEncounter(Encounter parentEncounter) {
        this.parentEncounter = null;
        this.parentEncounter = parentEncounter;
    }
    
    public void stopRunning() {
        this.running = false;
    }
    
    public void forceEncounterEnd() {
        this.running = false;
        this.encounterActive = false;
        this.forced = true;
        this.parentHandler.parent.addEncounter(this.parentEncounter, -1);
        if (Boolean.parseBoolean(this.parentHandler.parent.getProgramProperties().getProperty("sendData"))) {
            if (this.parentHandler.parent.commThread != null) {
                //this.parentHandler.parent.commThread.sendData("::endencounter::" + this.parentHandler.parent.characterID);
                Line line = new Line("", Line.ENCOUNTER_END);
                line.setCharacter(this.parentHandler.parent.getMainCharacter());
                line.setServer(this.parentHandler.parent.getServer());
                try {
                    this.parentHandler.parent.commThread.sendData(line);
                } catch (IOException ex) {
                    //nothing
                }
            }
        }
    }
    
    @SuppressWarnings("static-access")
    public void run() {
        //System.out.println("EncounterWatcher::run() - Encounter watcher started at " + new Date());
        this.parentHandler.parent.addPlaceHolder(this.parentEncounter.getMainEnemy());
        while (((count < (waitTime*2)) && (this.running)) && !forced) {
            try {
                if (this.running) {
                    this.parentHandler.parent.updateEncounterGraph(this.parentEncounter);
                }
                count++;
                this.currentThread().sleep(500);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        if (this.running && !this.forced) {
            this.parentHandler.parent.addEncounter(this.parentEncounter, -1);
            try {
                if (Boolean.parseBoolean(this.parentHandler.parent.getProgramProperties().getProperty("sendData"))) {
                    if (this.parentHandler.parent.commThread != null) {
                        //this.parentHandler.parent.commThread.sendData("::endencounter::" + this.parentHandler.parent.characterID);
                        Line line = new Line("", Line.ENCOUNTER_END);
                        line.setCharacter(this.parentHandler.parent.getMainCharacter());
                        line.setServer(this.parentHandler.parent.getServer());
                        this.parentHandler.parent.commThread.sendData(line);
                    }
                }
            } catch (IOException ex) {
                //nothing
            }
        }
        //System.out.println("EncounterWatcher::run() - Encounter watcher ended at   " + new Date());
        this.encounterActive = false;
    }
}
